<?php

/**
 * Description of users
 *
 * @author Lugol Metayer
 */
class Users extends Controller{

		function index(){
		$userslist = $this->users_info->retrieve_users();

		$data = array();

		$data["users"] = $userslist;
		$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                 $data['title'] = "L & R Users";
                
                 $this->load->view('user/index', $data);
                
		

		}
		function add(){

				if (isset($_POST['username']))
					{
							$this->form_validation->set_rules('username', 'UserName', 'required|trim|xss_clean|alpha_numeric');
							$this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean|matches[password2]|md5');
							$this->form_validation->set_rules('password2', 'Confirm Password', 'required|trim');
							$this->form_validation->set_rules('answer1', 'Security Answer 1', 'required');
							$this->form_validation->set_rules('answer2', 'Security Answer 2', 'required');
							$this->form_validation->set_rules('permission', 'Permission', 'required');

							if ($this->form_validation->run() == FALSE)
							{
									$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                                                                         $data['title'] = "L & R Add User";
                                                                        
                                                                         $this->load->view('user/adduser');
                                                                         
                                                                            
							}
							else
							{

									$uname = $_POST['username'];
									$pword = dohash($_POST['password'], 'md5');
									$ans1 = $_POST['answer1'];
									$ans2 = $_POST['answer2'];
									$perm = $_POST['permission'];

									$this->users_info->insert_users($uname,$pword,$ans1,$ans2,$perm);
									$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
									 $data['title'] = "L & R Veterinarian Clinic";
                                                                        
                                                                         $this->load->view('user/sucess');
                                                                         

							}
					}
					else{
							$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                                         $data['title'] = "L & R Add Users";
                                        
                                         $this->load->view('user/adduser');
                                         
					}
		}
		function update($wherestring){

		if (isset($_POST['username']))
					{
							$this->form_validation->set_rules('username', 'UserName', 'required|trim|xss_clean|alpha_numeric');
							$this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean|matches[password2]|md5');
							$this->form_validation->set_rules('password2', 'Confirm Password', 'required|trim');
							$this->form_validation->set_rules('answer1', 'Security Answer 1', 'required');
							$this->form_validation->set_rules('answer2', 'Security Answer 2', 'required');
							$this->form_validation->set_rules('permission', 'Permission', 'required');

							if ($this->form_validation->run() == FALSE)
							{
								$userslist = $this->users_info->retrieve_details($wherestring);

								$data = array();
								$data["user"] = $userslist['0'];
                                                                  $data['title'] = "L & R Users";
                                                                
                                                                 $this->load->view('user/edit', $data);
                                                                 
								
								
							}
							else
							{
									$uname = $_POST['username'];
									$pword = $_POST['password'];
									$ans1 = $_POST['answer1'];
									$ans2 = $_POST['answer2'];
									$perm = $_POST['permission'];
									$id = $wherestring;

									$this->users_info->update_users($uname,$pword,$ans1,$ans2,$perm,$id);

								 $data['title'] = "L & R Users";
                                                                
                                                                 $this->load->view('user/sucess');
                                                                 
								
							}
					}
					else{
					$userslist = $this->users_info->retrieve_details($wherestring);

					$data = array();
					$data["user"] = $userslist['0'];
                                         $data['title'] = "L & R Users";
                                        
                                         $this->load->view('user/edit', $data);
                                         
					
					}

		}
		function delete($wherestring){
		$this->users_info->delete_users($wherestring);
		$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                   $data['title'] = "L & R Users";
                $
                 $this->load->view('user/sucess', $data);
                 

		}
}
//*EOF*//
